#include "updatacourse.h"
#include "Settings.h"
#include <QtGui/QMessageBox>

UpdataCourse::UpdataCourse()
{
    this->setupUi(this);
    this->setWindowTitle(QString::fromUtf8("修改课程信息"));

    connect(this->pushButton_3,SIGNAL(clicked()),this,SLOT(deleteCourse()));//命名出现错误，这个才是delete按钮
    connect(this->bt_delete,SIGNAL(clicked()),this,SLOT(close()));
    connect(this->bt_updata,SIGNAL(clicked()),this,SLOT(updataCourse()));
}

void UpdataCourse::updataCourse()
{
    if(this->et_classname->text().trimmed().isEmpty())
    {
        QMessageBox::warning(this,QString::fromUtf8("警告！"),QString::fromUtf8("课程名字不能为空"));
        return;
    }
    int classid;
    if(!this->et_classID->text().trimmed().isEmpty())
        classid=(this->et_classID->text()).toInt();
    else classid=-1;
    QString teacher=this->et_teacher->text().trimmed();
    QString classroom=this->et_classroom->text().trimmed();
    if(classroom.isEmpty())
    {
        QMessageBox::warning(this,QString::fromUtf8("警告！"),QString::fromUtf8("授课地点不能为空"));
        return;
    }
    if(this->et_startweek->text().trimmed().isEmpty())
    {
        QMessageBox::warning(this,QString::fromUtf8("警告！"),QString::fromUtf8("第几周开始选项不能为空"));
        return;
    }
    int weekstart=this->et_startweek->text().trimmed().toInt();
    if(this->et_endweek->text().trimmed().isEmpty())
    {
        QMessageBox::warning(this,QString::fromUtf8("警告！"),QString::fromUtf8("第几周结束选项不能为空"));
        return;
    }
    int weekend=this->et_endweek->text().trimmed().toInt();
    if(this->et_dayofweek->text().trimmed().isEmpty())
    {
        QMessageBox::warning(this,QString::fromUtf8("警告！"),QString::fromUtf8("周几的课选项不能为空"));
        return;
    }
    int weekday=this->et_dayofweek->text().trimmed().toInt();
    if(this->et_startcoursenumber->text().trimmed().isEmpty())
    {
        QMessageBox::warning(this,QString::fromUtf8("警告！"),QString::fromUtf8("从第几节开始选项不能为空"));
        return;
    }
    int coursenumber=this->et_startcoursenumber->text().trimmed().toInt();
    if(this->et_coursecount->text().trimmed().isEmpty())
    {
        QMessageBox::warning(this,QString::fromUtf8("警告！"),QString::fromUtf8("共有几节课选项不能为空"));
        return;
    }
    int coursecount=this->et_coursecount->text().trimmed().toInt();
    int credit=-1;
    if(!this->et_coursecredit->text().trimmed().isEmpty())
    {
        credit=this->et_coursecredit->text().trimmed().toFloat();
    }
    QString otherinfo=this->et_otherinfo->text().trimmed();
    //更新课程信息
    //数据库写入
    QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");//数据库查询
    db.setHostName(HOSTNAME);
    db.setUserName(USERNAME);
    db.setPassword(PASSWORD);
    db.setDatabaseName(DATABASENAME);
    if(!db.open())
    {
        QMessageBox::critical(0,QObject::tr("数据库出错"),db.lastError().text());
        return ;
    }
    QString classnameold=classname;
    classname=this->et_classname->text().trimmed();
    QSqlQuery query;
    if(credit!=-1&&classid!=-1)
        query.exec("update classinfo set classname=\""+classname+"\", classid="+QString::number(classid)+" ,teacher=\""+teacher+"\", weekstart="+QString::number(weekstart)+", weekend="+QString::number(weekend)+", classroom=\""+classroom+"\", weekday="+QString::number(weekday)+", coursenumber="+QString::number(coursenumber)+", coursecount="+QString::number(coursecount)+", credit="+QString::number(credit)+", otherinfo=\""+otherinfo+"\" where classname=\""+classnameold+"\"");
    else
        if(credit!=-1&&classid==-1)
            query.exec("update classinfo set classname=\""+classname+"\", classid=NULL ,teacher=\""+teacher+"\", weekstart="+QString::number(weekstart)+", weekend="+QString::number(weekend)+", classroom=\""+classroom+"\", weekday="+QString::number(weekday)+", coursenumber="+QString::number(coursenumber)+", coursecount="+QString::number(coursecount)+", credit="+QString::number(credit)+", otherinfo=\""+otherinfo+"\" where classname=\""+classnameold+"\"");
    else
        if(credit==-1&&classid!=-1)
            query.exec("update classinfo set classname=\""+classname+"\", classid="+QString::number(classid)+" ,teacher=\""+teacher+"\", weekstart="+QString::number(weekstart)+", weekend="+QString::number(weekend)+", classroom=\""+classroom+"\", weekday="+QString::number(weekday)+", coursenumber="+QString::number(coursenumber)+", coursecount="+QString::number(coursecount)+", credit="+QString::number(credit)+", otherinfo=\""+otherinfo+"\" where classname=\""+classnameold+"\"");
    else
        query.exec("update classinfo set classname=\""+classname+"\", classid=NULL ,teacher=\""+teacher+"\", weekstart="+QString::number(weekstart)+", weekend="+QString::number(weekend)+", classroom=\""+classroom+"\", weekday="+QString::number(weekday)+", coursenumber="+QString::number(coursenumber)+", coursecount="+QString::number(coursecount)+", credit="+QString::number(credit)+", otherinfo=\""+otherinfo+"\" where classname=\""+classnameold+"\"");
    db.close();
    emit  updatafinish();
    this->close();
}

void UpdataCourse::deleteCourse()
{
    //删除该课程
    QMessageBox::StandardButton rb =QMessageBox::question(this,QString::fromUtf8("确定删除"),QString::fromUtf8("确定删除此课程记录吗?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
    if(rb == QMessageBox::Yes)
    {
        QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");//数据库查询
        db.setHostName(HOSTNAME);
        db.setUserName(USERNAME);
        db.setPassword(PASSWORD);
        db.setDatabaseName(DATABASENAME);
        if(!db.open())
        {
            QMessageBox::critical(0,QObject::tr("数据库出错"),db.lastError().text());
            return ;
        }
        QSqlQuery query;
        query.exec("delete from classinfo where classname=\""+classname+"\"");
        emit deletefinish();
        db.close();
        this->close();
    }
}

void UpdataCourse::loadinfo(QString cn)
{
    classname=cn;
    QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");//数据库查询
    db.setHostName(HOSTNAME);
    db.setUserName(USERNAME);
    db.setPassword(PASSWORD);
    db.setDatabaseName(DATABASENAME);
    if(!db.open())
    {
        QMessageBox::critical(0,QObject::tr("数据库出错"),db.lastError().text());
        return ;
    }
    QSqlQuery query;
    query.exec("select * from classinfo where classname=\""+classname+"\"");
    while(query.next())
    {
        this->et_classname->setText(query.value(0).toString());
        this->et_classID->setText(query.value(1).toString());
        this->et_teacher->setText(query.value(2).toString());
        this->et_startweek->setText(query.value(3).toString());
        this->et_endweek->setText(query.value(4).toString());
        this->et_classroom->setText(query.value(5).toString());
        this->et_dayofweek->setText(query.value(6).toString());
        this->et_startcoursenumber->setText(query.value(7).toString());
        this->et_coursecount->setText(query.value(8).toString());
        this->et_coursecredit->setText(query.value(9).toString());
        this->et_otherinfo->setText(query.value(10).toString());
    }
    db.close();
}

UpdataCourse::~UpdataCourse(){}
